﻿using System;
using System.Windows.Forms;
using NanoERP.Model;
using NanoERP.Business.BaseInfo;
using NanoERP.Common;

namespace NanoERP.BaseInfo
{
    //[Module("FrmDepartInfoEdit")]
    public partial class FrmDepartInfoEdit : Form
    {
        Deptinfo _deptinfo;
        readonly DeptinfoBLL _deptinfobLl;
        public FrmDepartInfoEdit()
        {
            InitializeComponent();
        }

        public FrmDepartInfoEdit(Deptinfo deptinfo, string parentName)
        {
            InitializeComponent();

            _deptinfobLl = new DeptinfoBLL();
            _deptinfo = deptinfo;
            txtParent.Text = parentName;

            BindFormControl();
        }

        void BindFormControl()
        {
            txtDeptname.DataBindings.Add(new Binding("Text", _deptinfo, "Deptname", true, DataSourceUpdateMode.OnPropertyChanged));
            txtDeptpym.DataBindings.Add(new Binding("Text", _deptinfo, "Deptpym", true, DataSourceUpdateMode.OnPropertyChanged));
            iiTag.DataBindings.Add(new Binding("ValueObject", _deptinfo, "Tag", true, DataSourceUpdateMode.OnPropertyChanged));
            txtNote.DataBindings.Add(new Binding("Text", _deptinfo, "Note", true, DataSourceUpdateMode.OnPropertyChanged));
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (txtDeptname.Text.Length == 0)
            {
                MessageBox.Show("部门名称不能为空，请输入.", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtDeptname.Focus();
                return;
            }

            _deptinfobLl.Edit(_deptinfo);

            MessageBox.Show("保存成功.", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            if (!chIsAdd.Checked)
            {
                this.DialogResult = DialogResult.OK;
                return;
            }
            ControlUtil.ClearControlBind(panel1);
            int id = _deptinfo.Parentdeptid.Value;
            _deptinfo = new Deptinfo { Parentdeptid = id };
            BindFormControl();
        }

        private void txtDeptname_Leave(object sender, EventArgs e)
        {
            txtDeptpym.Text = ChineseToSpell.GetChineseSpell(txtDeptname.Text);
        }
    }
}
